package com.qianchang.microfinance.view.home;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.BaseProduct;
import com.qianchang.microfinance.bean.CooperationBean;
import com.qianchang.microfinance.bean.Product;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseRecyclerAdapter;
import com.qianchang.microfinance.view.findings.MFindsAdapter;
import com.qianchang.microfinance.view.yfbpay.BankCardRvAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by SSC on 2017/12/18.
 */

public class ProductListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener{


    private Context mContext;
    private LayoutInflater inflater;
//    private List<Product> productList;
    private List<? extends BaseProduct> productList;
//    private List<CooperationBean> cooperationBeanList;
    private List<? extends BaseProduct> cooperationBeanList;
    private List<BaseProduct> baseProductList;
    private OnItemClickListener mOnItemClickListener = null;
    public final int PRODUCT_TYPE = 1;
    public final int COOPERATION_TYPE = 2;



    public ProductListAdapter(Context context){
        this.mContext = context;
        inflater = LayoutInflater.from(context);
        productList = new ArrayList<Product>();
        cooperationBeanList = new ArrayList<CooperationBean>();
        baseProductList = new ArrayList<BaseProduct>();
    }

    @Override
    public void onClick(View v) {
        if(mOnItemClickListener != null){
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }

    public static interface OnItemClickListener {
        void onItemClick(View view , int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public void addBaseProduct(List<BaseProduct> baseProdList){
        if(baseProdList != null){
            baseProductList.addAll(baseProdList);
        }
        notifyDataSetChanged();
    }



    public void setListProduct(List<Product> mproductList){
        if(mproductList != null){
            productList = mproductList;
            addBaseProduct((List<BaseProduct>) productList);
        }
        notifyDataSetChanged();
    }

    public void setCooperationBeanList(List<CooperationBean> mCooperationBeanList){
        if(cooperationBeanList != null){
            cooperationBeanList = mCooperationBeanList;
            addBaseProduct((List<BaseProduct>) cooperationBeanList);
        }
        notifyDataSetChanged();
    }


    public void clearData(){
        if(productList != null){
            productList.clear();
        }
        if(cooperationBeanList != null){
            cooperationBeanList.clear();
        }
        if(baseProductList != null){
            baseProductList.clear();
        }
        notifyDataSetChanged();
    }

    public final List<Product> getItems() {
        return (List<Product>) productList;
    }

    public final List<BaseProduct> getBaseTypeItems() {
        return baseProductList;
    }



    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if(viewType == PRODUCT_TYPE){
            View view = inflater.inflate(R.layout.item_product_layout,parent,false);
            ProductHolder productHolder = new ProductHolder(view);
            view.setOnClickListener(this);
            return productHolder;
        }
        if (viewType == COOPERATION_TYPE){
            View coopView = inflater.inflate(R.layout.item_cooperation_layout,parent,false);
            Cooperation productHolder = new Cooperation(coopView);
            coopView.setOnClickListener(this);
            return productHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if(holder instanceof ProductHolder){
            ProductHolder productHolder = (ProductHolder)holder;
//            Product product = (Product)productList.get(position);
            Product product = (Product)baseProductList.get(position);
            String memberLevel = product.getMemberLevel();
            Glide.with(mContext).load(product.getLogo()).error(R.drawable.img_jzsb).into(productHolder.titleImg);
            productHolder.projectName.setText(product.getProductName());
            if(product.getBizTime() != null){
                productHolder.bizTimeTv.setVisibility(View.VISIBLE);
                productHolder.bizTimeTv.setText(product.getBizTime() + "放款");
            }else{
                productHolder.bizTimeTv.setVisibility(View.INVISIBLE);
            }
            productHolder.monthRateTv.setText("月利率" + product.getMonthRate());
            productHolder.loanLimitTv.setText("贷款期限" + product.getLoanLimit());
            productHolder.overviewTv.setText(product.getOverview());


            if(memberLevel.equals("1")){
                Drawable drawable= mContext.getResources().getDrawable(R.drawable.tab_v1);
//            /// 这一步必须要做,否则不会显示.
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                productHolder.overviewTv.setCompoundDrawables(drawable,null,null,null);
            }else if(memberLevel.equals("2")){
                Drawable drawable= mContext.getResources().getDrawable(R.drawable.tab_v2);
//            /// 这一步必须要做,否则不会显示.
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                productHolder.overviewTv.setCompoundDrawables(drawable,null,null,null);
            }
            productHolder.amountLimitTv.setText(product.getAmountLimit());
            holder.itemView.setTag(position);
        }


        if(holder instanceof Cooperation){
            Cooperation cooperHolder = (Cooperation) holder;
            CooperationBean cooperation = (CooperationBean)baseProductList.get(position);
            Glide.with(mContext).load(cooperation.getImageUrl()).error(R.drawable.default_img).into(cooperHolder.ivProduct);
            cooperHolder.tvPrdName.setText(cooperation.getName());
            cooperHolder.tvExplain.setText(cooperation.getExplain());
            holder.itemView.setTag(position);
        }

    }

    @Override
    public int getItemCount() {
        if(baseProductList != null){
            return baseProductList.size();
        }
        return 0;
    }

    @Override
    public int getItemViewType(int position) {
        if(PRODUCT_TYPE == baseProductList.get(position).getBaseType()){
            return PRODUCT_TYPE;
        }else if(COOPERATION_TYPE == baseProductList.get(position).getBaseType()){
            return COOPERATION_TYPE;
        }
        return -1;
    }

    public class ProductHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.img_title_id)
        ImageView titleImg;
        @BindView(R.id.tv_productName_id)
        TextView projectName;
        @BindView(R.id.tv_amountLimit_id)
        TextView amountLimitTv;
        @BindView(R.id.tv_bizTime_id)
        TextView bizTimeTv;
        @BindView(R.id.tv_monthRate_id)
        TextView monthRateTv;
        @BindView(R.id.tv_loanLimit_id)
        TextView loanLimitTv;
        @BindView(R.id.tv_overview_id)
        TextView overviewTv;
        @BindView(R.id.tv_immediately_id)
        TextView immediatelyTv;

        public ProductHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public class Cooperation extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_ie_id)
        ImageView ivProduct;
        @BindView(R.id.tv_name_id)
        TextView tvPrdName;
        @BindView(R.id.tv_explain_id)
        TextView tvExplain;
        @BindView(R.id.tv_goBorw_id)
        TextView tvGoBorw;

        public Cooperation(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }




}
